
class ComposeStream: public std::ostream
{
    struct ComposeBuffer: public std::streambuf
    {
        void addBuffer(std::streambuf* buf)
        {
            buffers.push_back(buf);
        }
        virtual int overflow(int c)
        {
            //
            // XCode doesn't properly handle std::mem_fun member function pointers.
            //
            for(auto it = buffers.begin(); it != buffers.end(); it++)
            {
                (*it)->sputc(c);
            }
            //std::for_each(bufs.begin(),bufs.end(),std::bind2nd(std::mem_fun(&std::streambuf::sputc),c));
            return c;
        }

    private:
        std::vector<std::streambuf*> buffers;

    };

    ComposeBuffer myBuffer;

public: 
    ComposeStream()
        :std::ostream(NULL)
    {
        std::ostream::rdbuf(&myBuffer);
    }
    ComposeStream(std::ostream& s0)
        :std::ostream(NULL)
    {
        std::ostream::rdbuf(&myBuffer);
        linkStream(s0);
    }
    ComposeStream(std::ostream& s0, std::ostream& s1)
        :std::ostream(NULL)
    {
        std::ostream::rdbuf(&myBuffer);
        linkStream(s0);
        linkStream(s1);
    }
    void linkStream(std::ostream& out)
    {
        out.flush();
        myBuffer.addBuffer(out.rdbuf());
    }
};
